<template>
  <d2-container>
    <el-form :inline="true" :model="dataSearch" ref="dataSearch">
      <el-form-item label="教室名称" prop="name">
        <el-input v-model="dataSearch.name" placeholder="请输入教室名称"></el-input>
      </el-form-item>

                               <el-form-item label="校区" prop="school" v-if='showSchool'>
                <el-select
                        v-model="dataSearch.school"
                        filterable
                        placeholder="请选择校区"
                        @change="schoolSelected"
                        :disabled="disabled"
                >
                    <el-option
                            v-for="item in schoolList"
                            :key="item._id"
                            :label="item.name"
                            :value="item._id"
                    ></el-option>
                </el-select>
            </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="formSubmit">
          <d2-icon name="search" />查询
        </el-button>
      </el-form-item>
      <el-form-item>
        <el-button @click="formReset">
          <d2-icon name="refresh" />重置
        </el-button>
      </el-form-item>
      <a href="#/classroom-new">
        <el-button type="primary" style="float:right" icon="el-icon-plus">新建教室</el-button>
      </a>
    </el-form>
    <el-table :data="dataList" border style="width: 100%">
      <el-table-column prop="name" label="教室名称"></el-table-column>
      <el-table-column prop="number" label="可容纳学生"></el-table-column>
      <el-table-column prop="school" label="校区"></el-table-column>
      <el-table-column  label="操作">
        <template slot-scope="scope">
          <el-button @click="handleClick(scope.row)" type="text" size="small">查看</el-button>
          <el-button @click="deleteClick(scope.row)" type="text" size="small">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      layout="total,sizes,prev, pager, next,jumper"
      :page-size="pageSize"
      :current-page="currentPage"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handlerSizeChange"
      @next-click="handleNextPage"
      @prev-click="handlePrevClick"
    ></el-pagination>
  </d2-container>
</template>

<script>
import request from "@/plugin/axios";
import {mapState} from "vuex";

export default {
  data() {
    return {
      dataList: [],
      total: 0,
      pageSize: 10,
      currentPage: 1,
      classroom: [],
      dataSearch: {school :undefined},
      showSchool :false,
      headquarter : 0,
      schoolList : []
    };
  },
  computed: {
    ...mapState('d2admin/user', ['info']),
  },
  methods: {
    formSubmit() {
      this.dataSearch.page = this.currentPage;
      this.dataSearch.pageSize = this.pageSize;
      this.getDataList(this.dataSearch);
    },
    formReset() {
      this.$refs.dataSearch.resetFields();
      this.getDataList(this.dataSearch)
    },
    handleClick(e) {
      this.$router.push({ path: "/classroom-detail?id=" + e._id });
    },

    handleCurrentChange(currentPage) {
      this.currentPage = currentPage;
      this.dataSearch.page = this.currentPage;
      this.dataSearch.pageSize = this.pageSize;
      this.getDataList(this.dataSearch);
    },
    handlerSizeChange(pageSize) {
      this.pageSize = pageSize;
      this.dataSearch.page = this.currentPage;
      this.dataSearch.pageSize = this.pageSize;
      this.getDataList(this.dataSearch);
    },
    handleNextPage() {
      this.currentPage = this.currentPage + 1;
      this.dataSearch.page = this.currentPage;
      this.dataSearch.pageSize = this.pageSize;
      this.getDataList(this.dataSearch);
    },
    handlePrevClick() {
      this.currentPage = this.currentPage - 1;
      this.dataSearch.page = this.currentPage;
      this.dataSearch.pageSize = this.pageSize;
      this.getDataList(this.dataSearch);
    },
    deleteClick(e) {
      this.$confirm("此操作将删除该教室, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          request({
            url: "/classroom/deleteClassroom",
            method: "post",
            data: { id: e._id }
          }).then(res => {
            if (res.code == 200) {
              this.$message({
                message: "删除成功"
              });
              this.getDataList(this.dataSearch);
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },

    getDataList(data) {
      this.dataSearch.limitSchool = this.info.school.map(value => value._id);
      data.limitSchool = this.dataSearch.limitSchool;
      if (this.headquarter == 1){
        data.limitSchool = this.dataSearch.school
        data.school = this.dataSearch.school
      }
      request({
        url: "/classroom/getClassroomList",
        method: "post",
        data: data
      }).then(res => {
        this.dataList = [];
        this.dataList = res.data.classroom;
        this.dataList.forEach((item, index) => {
          this.dataList[index].school = item.school.name;
        });
        this.total = res.data.total;
        this.currentPage = res.data.currentPage;
      });
    },

                        getSchoolList() {
                let data = {};
                data.page = 1
                data.pageSize = 99999
                request({
                    url: "/school/getSchoolList",
                    method: "post",
                    data
                }).then(res => {
                    this.schoolList = res.data.school;
                    // 返回数据
                });
            },
            load(){
               this.dataSearch.page = this.currentPage;
        this.dataSearch.pageSize = this.pageSize;
        this.getDataList(this.dataSearch);
                    this.headquarter = ((this.info.school[0].headquarter == 1) || (this.info.school[0].headquarter == 2)) ? 1 : 0

        if (this.headquarter == 1){
                    this.getSchoolList()
                    this.showSchool = true
                    this.dataSearch.school = this.info.school[0]._id
                }
            }
  },
          beforeRouteEnter(to,from,next){
            if (to.meta.refresh == 'a'){
                to.meta.refresh = undefined
                next(v=>{
                v.load()
                })
            }
            else{
                next()
            }

        },
        
  created() {
    this.load()
  }
};
</script>
